package com.dtw.ditouwang.ui.more;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.dtw.ditouwang.R;
import com.dtw.ditouwang.beans.EditorChooseBean;
import com.dtw.ditouwang.beans.MoreBean;
import com.dtw.ditouwang.ui.BaseActivity;
import com.dtw.ditouwang.ui.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class EditorChooseNormalActivity extends BaseActivity implements OnItemClickListener {
    public static final String KeyID = "KeyID";
    public static final String KeyType = "keyType";
    public static final String KeyTitle = "KeyTitle";
    EditorChooseNormalAdapter editorChooseNormalAdapter;
    List<MoreBean.DataBean> dataBeanList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_editor_choose_normal_content);
        enableBack();
        setTitle(getIntent().getStringExtra(KeyTitle));
        final RecyclerView recyclerView = findViewById(R.id.recyclerview);
        editorChooseNormalAdapter = new EditorChooseNormalAdapter(dataBeanList);
        editorChooseNormalAdapter.setOnItemClickListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(editorChooseNormalAdapter);


        webApi.getEditorChooseNormal(getIntent().getIntExtra(KeyType, 1), getIntent().getIntExtra(KeyID, 1)).enqueue(new Callback<MoreBean>() {
            @Override
            public void onResponse(Call<MoreBean> call, Response<MoreBean> response) {
                if (response.body().isResult()&&!isFinishing()) {
                    dataBeanList.clear();
                    dataBeanList.addAll(response.body().getData());
                    editorChooseNormalAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<MoreBean> call, Throwable t) {

            }
        });
    }

    @Override
    public void onItemClick(int resID, int position, int state) {
        Intent intent = new Intent(this, EditorChooseNormalDetailActivity.class);
        intent.putExtra(EditorChooseNormalDetailActivity.KeySYSID,dataBeanList.get(position).getId());
        startActivity(intent);
    }
}
